#!/usr/bin/env perl
use warnings;
use strict;

my $ts_by_user = "user_residuals.txt";
my $ts_by_movie = "user_residuals_by_movie.txt";

my @smoothed = ();

sub smooth_users {
  my ($similarity_bin, $min_ratings) = @_; 
  $similarity_bin =~ /(.*)\.similarity\.bin/ or die "bad: $similarity_bin";
  my $out = "smooth_users_$similarity_bin.$min_ratings.txt";
  my $err = "$out.err";
  push @smoothed, $out;
print <<STR;
$out: $similarity_bin $ts_by_user
\tbin/smooth_users 17770 $similarity_bin $min_ratings <$ts_by_user >$out 2>$err
STR
}

sub smooth_movies {
  my ($similarity_bin, $min_ratings) = @_; 
  $similarity_bin =~ /(.*)\.similarity\.bin/ or die "bad: $similarity_bin";
  my $out = "smooth_movies_$similarity_bin.$min_ratings.txt";
  my $err = "$out.err";
  push @smoothed, $out;
print <<STR;
$out.by_movie: $similarity_bin $ts_by_movie
\tbin/smooth_movies 17770 $similarity_bin $min_ratings <$ts_by_movie >\$@ 2>$err
$out: $out.by_movie
\tbash src/invert_ratings.sh <$out.by_movie >$out
STR
}

my @sim_files = (
  "bellkor_pearson.similarity.bin",
  "link_noredir_0.new.similarity.bin",
);
#push @sim_files, glob("*.similarity.bin");

for my $sim (@sim_files) {
  smooth_movies($sim, 200);
  smooth_movies($sim, 100);
  smooth_users($sim, 50);
  smooth_users($sim, 20);
}

# Rules to make all smoothed training sets.
my $targets = join(" ", @smoothed);
print <<EOF;
smooth: $targets
EOF

# Copyright (c) 2009 John Lees-Miller
# 
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
# 
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

